package com.store.bookstore_backend.web.controller;


import com.store.bookstore_backend.dto.*;
import com.store.bookstore_backend.service.BookstoreOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/order")
public class BookStroreOrderController {
    @Autowired
    BookstoreOrderService bookstoreOrderService;

    //获取用户订单列表
    @GetMapping
    public APIResponse<List<OrderDTO>> getOrder(HttpServletRequest request
    ) {
        return APIResponse.success(bookstoreOrderService.getOrders(request));
    }

    //获取订单详情
    @GetMapping("/{orderId}")
    public APIResponse<OrderInfoDTO> getOrderById(HttpServletRequest request
            , @PathVariable("orderId") String orderId) {
        return APIResponse.success(bookstoreOrderService.getOrderById(request, orderId));
    }


    //创建订单
    @PostMapping
    public APIResponse<OrderInfoDTO> createOrder(HttpServletRequest request, @RequestBody CreateOrderDTO createOrderDTO) {
        return APIResponse.success(bookstoreOrderService.createOrder(request, createOrderDTO));
    }

    //关闭订单
    @PutMapping("/close/{orderId}")
    public APIResponse<Object> closeOrder(HttpServletRequest request
            , @PathVariable("orderId") String orderId) {
        bookstoreOrderService.closeOrder(request, orderId);
        return APIResponse.success();
    }

    //支付订单
    @PutMapping("/pay")
    public APIResponse<Object> payOrder(HttpServletRequest request
            , @RequestBody PayOrderDTO payOrderDTO) {
        return APIResponse.success(bookstoreOrderService.payOrder(request, payOrderDTO));
    }

    //支付成功
    @PutMapping("/paySuccess")
    public APIResponse<Object> paySuccess(@RequestBody PaySuccessDTO paySuccessDTO) {
        bookstoreOrderService.paySuccess(paySuccessDTO);
        return APIResponse.success();
    }

    //确认收货
    @PutMapping("/complete/{orderId}")
    public APIResponse<Object> complete(@PathVariable("orderId") String orderId) {
        bookstoreOrderService.orderComplete(orderId);
        return APIResponse.success();
    }
}
